<?php
//var_dump($_SERVER);
//echo "server ".$_SERVER['DOCUMENT_ROOT']."/videoToulouse/sources/dao/";
//$ch1 = $_SERVER['DOCUMENT_ROOT']."/videoToulouse/sources/dao/";
require_once 'constantes.php';
require_once 'DaoConn.class.php';


class DaoClients extends DaoConn {
	
	function getClientById($id) {
		$this->open();
		try {
			$sql = "SELECT * FROM ".CLIENTS." WHERE ".CLIENTS_ID." =:id;";
			$req = $this->bdd->prepare($sql);
			$req->execute(array('id'=>$id));
			$result = $req->fetch(PDO::FETCH_OBJ);
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}		
		$this->close();
		return $result;
	}	
	
	function getClientAll() {
		$this->open();
		try {
			$sql = "SELECT * FROM ".CLIENTS.";";
			$req = $this->bdd->prepare($sql);
			$req->execute();
			$tab = array();
			while($result = $req->fetch(PDO::FETCH_OBJ)){
				array_push($tab, $result);
			}
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();	
		return $tab;
	}	
	
	function isClient($id,$mdp){
		$bool = 0;// par defaut ce n'est pas un client
		$this->open();
		try {
			$sql = "SELECT * FROM ".CLIENTS." WHERE ".CLIENTS_ID." =:log AND ".CLIENTS_PWD." =:pass;";
			//echo "requete : ".$sql;
			$req = $this->bdd->prepare($sql);
			$req->execute(array('log'=>$id,'pass'=>$mdp));
			while($result = $req->fetch(PDO::FETCH_OBJ)){
					if(($result->cl_id == $id) && ($result->cl_pwd == $mdp) ){
						$bool = 1;// on la trouver
						
					}
				
			}
			 
			$req->closeCursor();
		} catch (Exception $e) {
			die('ERREUR SQL : '.$e->getMessage());
		}
		$this->close();
		return $bool;
	}
}	